package com.taxi.driver.module.account.code;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.event.UserEvent;
import com.taxi.driver.module.account.code.dagger.CodeModule;
import com.taxi.driver.module.account.code.dagger.DaggerCodeComponent;
import com.taxi.driver.module.account.newpwd.NewPwdActivity;
import com.taxi.driver.widget.code.CodeInput;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：验证码输入页
 */
public class CodeActivity extends BaseActivity implements CodeContract.View {

    /**
     * @param context
     * @param phone
     * @param idCard
     */
    public static void actionStart(Context context, String phone, boolean isFirstLogin, String idCard) {
        Intent intent = new Intent(context, CodeActivity.class);
        intent.putExtra(IConstants.PHONE, phone);
        intent.putExtra(IConstants.ISFIRSTLOGIN, isFirstLogin);
        intent.putExtra(IConstants.IDCARD, idCard);
        context.startActivity(intent);
    }

    @BindView(R.id.img_head_left)
    ImageView mImgHeadLeft;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_notice)
    TextView mTvNotice;
    @BindView(R.id.code_input)
    CodeInput mCodeInput;
    @BindView(R.id.tv_resend)
    TextView mTvResend;

    String mPhone;
    boolean mIsFirstLogin;
    String mIdCard;

    @Inject
    CodePresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code);
        ButterKnife.bind(this);
        DaggerCodeComponent.builder().appComponent(getAppComponent())
                .codeModule(new CodeModule(this)).build().inject(this);

        mPhone = getIntent().getStringExtra(IConstants.PHONE);
        mIsFirstLogin = getIntent().getBooleanExtra(IConstants.ISFIRSTLOGIN, false);
        mIdCard = getIntent().getStringExtra(IConstants.IDCARD);
        mTvTitle.setText(mIsFirstLogin ? R.string.first_login : R.string.forget_pwd_title);
        mCodeInput.setCodeInputListener(() -> { //监听输入是否完整
            mPresenter.verifyCode(mPhone, mCodeInput.getCode());
        });
        mImgHeadLeft.setOnClickListener(v -> {
            finish(); //关闭界面
        });
        EventBus.getDefault().register(this);
        /* 发送验证码 */
        mPresenter.sendCode(mPhone);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserEvent(UserEvent event) {
        switch (event.type) {
            case UserEvent.RESET_PWD_SUCCESS:
                finish(); //关闭界面
                break;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        mPresenter.onDestory();
    }

    @OnClick(R.id.tv_resend)
    public void onClick() {
        mPresenter.sendCode(mPhone);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void sendCodeSuccess() {
        mTvNotice.setText(getString(R.string.code_notice, mPhone));
    }

    @Override
    public void setTimerDisplay(int remainTime) {
        if (remainTime > 0) {
            mTvResend.setText(getString(R.string.code_time, String.valueOf(remainTime)));
            mTvResend.setEnabled(false);
        } else {
            mTvResend.setText(R.string.code_resend);
            mTvResend.setEnabled(true);
        }
    }

    @Override
    public void verifyCodeSuccess(String code) {
        NewPwdActivity.actionStart(this, mPhone, mIsFirstLogin, mIdCard, code);
    }

    @Override
    public void resetCodeInput() {
        mCodeInput.resetInput();
    }

}
